package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    public AuthInterceptor authInterceptor() {
        return new AuthInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration loginInterceptorReg = registry.addInterceptor(loginInterceptor());
        loginInterceptorReg.addPathPatterns("/api/config/**");
        loginInterceptorReg.excludePathPatterns("/api/config/login", "/api/config/login/**", "/api/config/logout", "/api/config/toLogin", "/api/config/isLogined",
                "/swagger-ui.html", "/swagger-resources/**", "/v2/api-docs", "/");

        InterceptorRegistration authInterceptorReg = registry.addInterceptor(authInterceptor());
        authInterceptorReg.addPathPatterns("/api/config/**");
        authInterceptorReg.excludePathPatterns("/api/config/login", "/api/config/login/**", "/api/config/logout", "/api/config/toLogin", "/api/config/isLogined",
                "/swagger-ui.html", "/swagger-resources/**", "/v2/api-docs", "/");
    }

    // 视图映射
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("register");
    }
}
